package jian.thread.newbie;

public class TestVolatile {

	static int i = 0, j = 0;

	public synchronized static void add() {
		i++;
		j++;
	}

	public static void print() {
		if (i != j) {
			System.out.println("i=" + i + "---j=" + j);
		}
	}

	public static void main(String[] args) {
		for (int m = 0; m < 10; m++) {
			Runnable run = new Runnable() {

				@Override
				public void run() {
					TestVolatile.add();
				}
			};

			Runnable run2 = new Runnable() {

				@Override
				public void run() {
					TestVolatile.print();
				}
			};
			for (int i = 0; i < 100; i++) {
				if (Math.random() * 10 > 5) {
					new Thread(run).start();
				} else {
					new Thread(run2).start();
				}
			}
		}

	}
}
